﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace Vav.Api.Utils
{
    public class IdCardEncryptExtensions
    {  //密钥
        private static string sKey = "HfvvpOi5MQLMRmSmFBi6MBcrPesDXzuC";
        //矢量，矢量可以为空 12位
        private static string sIV = "7XcMIOqWreE=";
        public static string CheckDataEncrypt(string str)
        {
            string postdata = System.Web.HttpUtility.UrlEncode(EncryptString(str));
            return postdata;
        }
        public static string EncryptString(string value)
        {
            if (!String.IsNullOrEmpty(value))
            {
                SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider();
                ICryptoTransform ct;
                MemoryStream ms;
                CryptoStream cs;
                byte[] byt;
                mCSP.Key = Convert.FromBase64String(sKey);
                mCSP.IV = Convert.FromBase64String(sIV);
                //指定加密的运算模式
                mCSP.Mode = System.Security.Cryptography.CipherMode.ECB;
                //获取或设置加密算法的填充模式
                mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
                ct = mCSP.CreateEncryptor(mCSP.Key, mCSP.IV);
                byt = Encoding.UTF8.GetBytes(value);
                ms = new MemoryStream();
                cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
                cs.Write(byt, 0, byt.Length);
                cs.FlushFinalBlock();
                cs.Close();
                return Convert.ToBase64String(ms.ToArray());
            }
            else
            {
                return "";
            }
        }

        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string DecrypString(string source)
        {
            if (!String.IsNullOrEmpty(source))
            {
                try
                {
                    SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider();
                    byte[] bytIn = Convert.FromBase64String(source);
                    MemoryStream ms = new MemoryStream(bytIn, 0, bytIn.Length);
                    mCSP.Mode = System.Security.Cryptography.CipherMode.ECB;
                    //获取或设置加密算法的填充模式
                    mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
                    mCSP.Key = Convert.FromBase64String(sKey);
                    mCSP.IV = Convert.FromBase64String(sIV);
                    ICryptoTransform encrypto = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV);
                    CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);
                    StreamReader sr = new StreamReader(cs);
                    return sr.ReadToEnd();
                }
                catch
                {
                    return "";
                }
            }
            else
            {
                return "";

            }
        }
    }
}
